package com.wlscale.roadsystem.modular;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.wlscale.roadsystem.R;
import com.wlscale.roadsystem.base.LZBaseActivity;
import com.wlscale.roadsystem.modular.setting.SettinActivity;
import com.wlscale.roadsystem.wiget.BatteryView;
import com.wlscale.roadsystem.wiget.KeyboardPopupWindow;
import com.wlscale.roadsystem.wiget.SwitchingUsersDialog;

public class MainActivity extends LZBaseActivity implements View.OnClickListener {
    /**
     * 菜单
     */
    private Button btn_menu;
    /**
     * 切换用户
     */
    private Button btn_switch_user;


    private EditText mEditText;
    private RelativeLayout container;
    private KeyboardPopupWindow keyboardPopupWindow;

    private final int REQUEST_LPR_CODE = 100;

    @Override
    public void initlayoutResID() {
        setContentView(R.layout.activity_main);
    }

    @Override
    public void initBundle() {
        requestDangerousPermissions();

    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void initView() {
        mEditText = findViewById(R.id.test);
        container = findViewById(R.id.container);
        btn_menu = findViewById(R.id.btn_menu);
        btn_switch_user = findViewById(R.id.btn_switch_user);
        btn_menu.setOnClickListener(this);
        btn_switch_user.setOnClickListener(this);
        BatteryView bv_top = findViewById(R.id.bv_top);
        bv_top.setColor(getResources().getColor(R.color.colorGreen));
        bv_top.setPower(90);

        mEditText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                keyboardPopupWindow.show(container);

                return false;
            }
        });
        findViewById(R.id.brn_lpr).setOnClickListener(v -> {
            startActivityForResult(new Intent(this, LPRActivity.class), REQUEST_LPR_CODE);
        });
    }

    @Override
    public void initHeader() {


    }

    @Override
    public void initLoadingView() {
        keyboardPopupWindow = new KeyboardPopupWindow(this, mEditText, new KeyboardPopupWindow.ReslutOnClickLister() {
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_menu:
                goActivity(SettinActivity.class);
                break;
            case R.id.btn_switch_user:
                SwitchingUsersDialog dialog = new SwitchingUsersDialog.Builder().setContext(MainActivity.this).setOnClickLister(new SwitchingUsersDialog.OnClickLister() {
                    @Override
                    public void onClickLister(String locAddress) {

                    }
                }).builder();
                dialog.show();
                break;
        }
    }

    @SuppressLint("ShowToast")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 识别成功回调，车牌识别
        if (requestCode == REQUEST_LPR_CODE && resultCode == RESULT_OK) {
            if (data != null) {
                String card = data.getStringExtra("card");
                mEditText.setText(card);
//                new AlertDialog.Builder(this)
//                        .setMessage(card)
//                        .setNegativeButton("OK", (dialog, which) -> {
//                        })
//                        .show();
//                Toast.makeText(this, card, Toast.LENGTH_SHORT);
            }
        }
    }

    /**
     * 请求权限
     */
    public void requestDangerousPermissions() {
        String[] strings = new String[]{Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE};
        ActivityCompat.requestPermissions(this, strings, 100);
    }
}
